package sky.teleport_bow.commonds;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import sky.teleport_bow.utility.BowUtility;

public class Bow implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {

        if (commandSender instanceof Player p) {
            if (p.hasPermission("teleport_bow.bow")) {

                ItemStack bow = BowUtility.createBow();
                if (strings.length == 0) {

                    p.getInventory().addItem(bow);
                    p.getInventory().addItem(new ItemStack(Material.ARROW, 1));

                    p.sendMessage(ChatColor.GREEN + "你已经获取了传送弓，你知道该怎么用的");
                } else {
                    Player target = Bukkit.getPlayerExact(strings[0]);

                    if (target == null) {
                        p.sendMessage(ChatColor.RED + "无法定位到该玩家，该玩家可能不存在");
                        return true;
                    }


                    target.getInventory().addItem(bow);
                    target.getInventory().addItem(new ItemStack(Material.ARROW, 1));

                    target.sendMessage(ChatColor.GREEN + "你已经获取了传送弓，你知道该怎么用的");
                }
            }

        }


        return true;
    }
}
